//
//  DeviceManager.m
//  IOS_SST
//
//  Created by lfy on 16/4/25.
//  Copyright © 2016年 jiahua. All rights reserved.
//

#import "DeviceManager.h"
#import "SstWifi.h"
#import "KeyManage.h"
#import "sstNSStringFormat.h"
static DeviceManager * instance = nil;

@interface DeviceManager()
{
}
@property NSMutableArray *sstDeviceArray;
@property SstWifi *wifi;

@end
@implementation DeviceManager
//获取单例
+(DeviceManager *)Instance
{
    @synchronized(self) {
        if (instance == nil)
            instance = [[self alloc] init];
    }
    return instance;
}

-(id)init
{
    if(self = [super init])
    {
       self.sstDeviceArray = [NSMutableArray arrayWithCapacity : MAX_DEVICE_COUNT];
       self.wifi = [[SstWifi alloc] init];
    }
    return self;
}

-(void)setSession:(NSString *)sessionID withDeviceIDs:(NSArray *)devices{
    
    self.sessionID = sessionID;
    
    for(NSString *deviceId in devices)
    {
        SstDevice *device = [self getSstDeviceByDeviceId: deviceId];
        if (device == nil)
            device = [[SstDevice alloc] init];
        
        device.deviceID = deviceId;
        u8 udpKeyId[UDP_KEY_ID_LEN]  = {0};
        [KeyManage deviceId2UdpId: [sstNSStringFormat NSString2Char: deviceId] udpKeyId: udpKeyId];

        device.hexUdpKeyID = udpKeyId;
        [self addDevice: device];
    }
}

-(SstDevice *)getSstDeviceByUdpID: (NSString *)udpID
{
    SstDevice *device = NULL;
    for(SstDevice *dev in self.sstDeviceArray)
    {
        NSString *hexUdpKeyIdStr = [sstNSStringFormat Char2NSString: device.hexUdpKeyID];
        if (dev != nil && [hexUdpKeyIdStr isEqualToString: udpID])//TODO
        {
            device = dev;
            break;
        }
    }
    return device;
}

-(SstDevice *)getSstDeviceByIp: (NSString *)ip
{
    SstDevice *device = NULL;
    for(SstDevice *dev in self.sstDeviceArray)
    {
        if (dev != nil && [dev.ip isEqualToString: ip])
        {
            device = dev;
            break;
        }
    }
    return device;
}

-(SstDevice *)getSstDeviceByDeviceId: (NSString *)deviceId
{
    SstDevice *device = NULL;
    for(SstDevice *dev in self.sstDeviceArray)
    {
        if (dev != nil && [dev.deviceID isEqualToString: deviceId])
        {
            device = dev;
            break;
        }
    }
    return device;
}
/**
 *  设置无线网
 *
 *  @param ssid     无线网账户
 *  @param password 无线网密码
 */
-(void)setWIFIwithSSID:(NSString*) ssid withPassword:(NSString*)password
{
    self.wifi.ssid = ssid;
    self.wifi.password = password;
}

/**
 *  设置基本ip、Mac和DeviceID
 *
 *  @param deviceID <#deviceID description#>
 *  @param mac      <#mac description#>
 *  @param ip       <#ip description#>
 */
-(void)setDeviceInformation:(NSString *) deviceID withMac:(NSString *)mac withIP:(NSString*)ip
{
    SstDevice *device = [self getSstDeviceByIp: ip];
    if (device == nil)
    {
        device = [[SstDevice alloc] init];
    }
    device.deviceID = deviceID;
    device.mac = mac;
    device.ip = ip;
    [KeyManage generatorR3WithSSID: device withWifi: self.wifi];
    
    [self addDevice: device];
}

-(void)addDevice: (SstDevice *)device
{
    [self.sstDeviceArray addObject:device];
}

-(Boolean)getIsLegacyDevice: (NSString *)ip
{
    SstDevice *device = [self getSstDeviceByIp: ip];
    if (device != NULL) {
        return device.isLegacy;
    }
    return FALSE;
}

-(void)setIsLegacyDeviceByIp: (NSString *)ip
{
    SstDevice *device = [self getSstDeviceByIp: ip];
    if (device != NULL)
    {
        device.isLegacy = true;
    }
    else
    {
        device = [[SstDevice alloc] init];
        device.isLegacy = true;
        device.ip = ip;
        [self addDevice: device];
    }
}

-(void)setDeviceUdpKeyIdByIP: (NSString *)ip newUdpKeyId: (u8 *) udpKeyIdStr
{
    SstDevice *device = [self getSstDeviceByIp: ip];
    if (device == nil)
        return;
    
    device.hexUdpKeyID = udpKeyIdStr;
    Boolean isGetR3 = ![KeyManage isZeroTail: udpKeyIdStr];
    [self setDeviceHasGetR3: device hasGetR3: isGetR3];
}

-(void)setDeviceHasGetR3:(SstDevice *)device hasGetR3: (Boolean) get
{
    if (!device.hasGetR3 && get) {
        [KeyManage getTokenForDeviceFromCloud: device];
    }
}
@end
